//login.js
var util = require('../../utils/util.js');
var user = require('../../services/user.js');
//获取应用实例
var app = getApp();
Page({
  data: {
    remind: '加载中',
    angle: 0,
    userInfo: {},
    canIUse: wx.canIUse('getUserInfo')
  },
  goToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index',
    });
  },
  onLoad: function () {
    var that = this;
      wx.setNavigationBarTitle({
        title: 'AR智能机器人'
      })
      // 查看是否授权
      //wx.getSetting({
      //   success: function (res) {
      //     if (res.authSetting['scope.userInfo']) {
      //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称
      //       wx.getUserInfo({
      //         success: function (res) {
      //           console.log(res.userInfo)
      //         }
      //       })
      //     }
      //   }
      // })
  },
  onShow: function () {

  },
  onReady: function () {
    var that = this;
    setTimeout(function () {
      that.setData({
        remind: ''
      });
    }, 1000);
  },
  wxLogin: function () {
    user.checkLogin().then((err) => {
      wx.hideLoading();
      app.globalData.hasLogin = true;
      console.log("checklogin " + err);
      wx.switchTab({
        url: "/pages/index/index"
      })
    }).catch((res) => {
      user.loginByWeixin().then((res) => {
        wx.hideLoading();
        app.globalData.hasLogin = true;
        console.log(res);
        wx.switchTab({
          url:"/pages/index/index"
        })
      }).catch((err) => {
        wx.hideLoading();
        console.error(err);
        app.globalData.hasLogin = false;
        wx.showModal({
          title: '注意',
          content: '服务器出小差了，重启小程序后重试。'+err,
          showCancel: false
        });
      });
    })




  },
  bindGetUserInfo: function (e) {
    this.wxLogin();
    wx.switchTab({
     url: '/pages/index/index',
   });
  }
});